<?php


namespace app\api\controller;
use app\api\model\Base as bs;
use think\Db;
use think\Request;

class Useredit
{

    public function checkUserName(){
        $username = I('username');
        $data = Db::name('user')
            ->where('username',$username)
            ->where('status',1)
            ->find();
        if(!empty($data)){
            $this->apiReturn(303,'有同名',['success'=>true]);
        }else{
            $this->apiReturn(200,'没有同名',['success'=>false]);
        }
    }
    public function checkStudentNumber(){
        $studentNumber = I('student_number');
        $data = Db::name('user')
            ->where('student_number',$studentNumber)
            ->where('status',1)
            ->find();
        if(!empty($data)){
            $this->apiReturn(303,'有同名',['success'=>true]);
        }else{
            $this->apiReturn(200,'没有同名',['success'=>false]);
        }
    }
    public function getUser(){
        $id = I('id');
        $data = bs::selectId('user','id,username,student_number,mobile,email',$id);
        $total = bs::total('user');
        $this->apiReturn(200,'',$data,$total);
    }
    public function emailpassword(){
        $token = I('token');
        $password = user_md5(I('password'));
        $flag = Db::name('user')->where(['token'=>$token])->update(['password'=>$password]);
        if($flag)
        {
            $this->apiReturn(200,'修改成功',['success'=>$flag]);
        }else{
            $this->apiReturn(200,'修改失败',['success'=>$flag]);
        }

    }

    public function editpassword(){
        $id = I('id');
        $password = user_md5(I('password'));
        $flag = Db::name('user')->where(['id'=>$id])->update(['password'=>$password]);
        if($flag)
        {
            $this->apiReturn(200,'修改成功',['success'=>$flag]);
        }else{
            $this->apiReturn(200,'修改失败',['success'=>$flag]);
        }

    }
    public function userEdit(){
        $user = [
            'id' => input('id'),
            'mobile'=>input('mobile'),
            'email'=>input('email'),
            'create_time'=>time(),
            'update_time'=>time()
        ];

//        $result = $this->validate($user,'Vregist');
//        if(true!==$result){
//            $this->apiReturn(303,'验证错误');
//        }else{
        $flag = Db::name('user')->where(['id'=>$user['id']])->update($user);
        $this->apiReturn(200,'修改成功',['success'=>$flag]);
//        }
    }
    public function apiReturn( $code=200 , $msg='', $data = array() ,$total=0 ){
        $returnData = array(
            'code' => $code,
            'msg'  => $msg?$msg:return_msg($code),
            'data' => $data,
            'total'=>$total
        );
        if(I('callback')){
            echo I('callback').'('.json_encode($returnData).')';die;
        }
        echo json_encode($returnData);die;
    }
}